package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.mapper.ProductMapper;
import cn.tedu.coolshark.pojo.dto.ProductDTO;
import cn.tedu.coolshark.pojo.dto.ProductUpdateDTO;
import cn.tedu.coolshark.pojo.entity.Product;
import cn.tedu.coolshark.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Value("${imagesPath}")
    private String dirPath;

    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("insert")
    public void insert(@RequestBody ProductDTO productDTO) {
        System.out.println("productDTO = " + productDTO);
        Product p = new Product();
        BeanUtils.copyProperties(productDTO, p);
        p.setCreated(new Date());
        mapper.insert(p);
    }

    @RequestMapping("selectAdmin")
    public List<ProductAdminVO> selectAdmin() {
        return mapper.selectAdmin();
    }

    @RequestMapping("delete")
    public void delete(int id) {
        String url = mapper.selectUrlById(id);
        new File(dirPath + url).delete();
        mapper.deleteById(id);
    }

    @RequestMapping("selectUpdateById")
    public ProductUpdateVO selectUpdateById(int id) {
        return mapper.selectUpdateById(id);
    }

    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDTO productUpdateDTO) {
        System.out.println("productUpdateDTO = " + productUpdateDTO);
        String url = mapper.selectUrlById(productUpdateDTO.getId());
        /* 判断如果路径和原来路径不相等则删除原来路径对应的图片 */
        if (!url.equals(productUpdateDTO.getUrl())) {
            new File(dirPath + url).delete();
        }
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO, product);
        mapper.update(product);
    }

    @RequestMapping("selectTop")
    public List<ProductTopVO> selectTop() {
        return mapper.selectTop();
    }

    @RequestMapping("selectIndex")
    public List<ProductIndexVO> selectIndex() {
        return mapper.selectIndex();
    }

    @RequestMapping("selectDetailById")
    public ProductDetailById selectDetailById(int id, HttpSession session) {
        String info = (String) session.getAttribute("view" + id);
        if (info == null) {
            mapper.updateViewCountById(id);
            session.setAttribute("view"+id,"visited");
        }
        return mapper.selectDetailById(id);
    }

    @RequestMapping("selectByCId")
    public List<ProductIndexVO> selectByCId (int id){
        return mapper.selectByCId(id);
    }

    @RequestMapping("selectByWd")
    public List<ProductIndexVO> selectByWd(String wd){
        return mapper.selectByWd(wd);
    }
}
